package com.study.a19Max;

import java.util.ArrayList;
import java.util.Collections;

public class MyThread extends Thread{
    private ArrayList<Integer> list;
    public MyThread(ArrayList<Integer> list){
        this.list=list;
    }

    static ArrayList<Integer> list1=new ArrayList<>();
    static ArrayList<Integer> list2=new ArrayList<>();

    @Override
    public void run() {
        while(true){
            synchronized (com.study.a18choujiangxiang.MyThread.class){
                if(list.isEmpty()){
                    if(list.size()==0){if("抽奖箱1".equals(getName())){
                        System.out.println("抽奖箱1产生"+list1);
                        int sum=0;
                        for(int i:list1){
                            sum+=i;
                        }
                        System.out.println("抽奖箱1产生"+sum+"奖金");
                    }else{
                        System.out.println("抽奖箱2产生"+list2);
                        int sum=0;
                        for(int i:list2){
                            sum+=i;
                        }
                        System.out.println("抽奖箱2产生"+sum+"奖金");
                    }}
                    break;
                }else{
                    Collections.shuffle(list);
                    int prize=list.remove(0);
                    if("抽奖箱1".equals(getName())){
                        list1.add(prize);
                    }else{
                        list2.add(prize);
                    }
                    //System.out.println(getName()+"产生"+prize+"奖金");
                }
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
    }

}
